package com.fairytask.ui
{
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.objects.Player;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.MoneyTools;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.TopUpRewardItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 *充值奖励的ui的界面 
	 * @author yangj
	 * 
	 */	
	public class TopUpRewardUI	extends FrameUI
	{
		public static var _self		:TopUpRewardUI;
		public var topLabel			:Label;					//用于显示顶部的文字
		public var downLabel			:Label;					//用于显示需要多少仙玉可以领取
		public var topButton			:Button;				//用于连接充值的按钮
		public var viewGroupBG			:MovieClip;				//用于显示获得物品的viewgroup的背景
		public var viewGroup			:ViewGroup;				//用于显示物品
		
		private var valuePhoto			:Clip	=	new Clip();
		private var _itemArray			:Array = [];			//用于存放不同类型奖励的数组
		private var _num				:int;					//对传过来的数据进行计数，当道达一定的数量的时候进行再舞台上统一的显示
		private var _TypeListArray		:Array = [];			//类型显示
		private var _silver			:int;					//玩家充值的仙玉数量
		
		private var _nowType			:int = -1;				//存储当前选中的id
		private var _typeId				:String = "";				//储存当前的key	
		/**
		 *获得单例的模式 
		 * @return 
		 * 
		 */		
		public static function getInstance():TopUpRewardUI
		{
			if(_self ==null)
			{
				_self = new TopUpRewardUI();
			}
			return _self;
		}
		public function TopUpRewardUI(popUI:Boolean = true)
		{
			super(popUI);
			init();
			initEvent();
		}
		
		/**
		 *初始化显示 
		 * 
		 */		
		private function init():void
		{
			var list:String = StaticIni.getIniVar("TopUpRewardList","Value");
			_TypeListArray = list.split(",");
			valuePhoto.x = 650;
			valuePhoto.y = 200;
			addChild(valuePhoto);
			downLabel
			
		}
		
		/**
		 *初始化事件 
		 * 
		 */		
		private function initEvent():void
		{
			topButton.addEventListener(MouseEvent.CLICK,openWeb);
			//bindManager.BindProp(Player.getInstance().data,"GoldExp",this,"pGoldExp");
			//对充值表进行绑定
			bindManager.BindRecord(model.scene,RecordDefine.AWARDGETREC,onAddAwardGetRec,onremoveAwardGetRec);
			viewGroup.addItemsEvent(MouseEvent.ROLL_OVER,onShowTip);
			viewGroup.addItemsEvent(MouseEvent.ROLL_OUT,onHideTip);
		}
		
		/**z
		 *显示tips 
		 * @param evt
		 * @param index
		 * 
		 */		
		private function onShowTip(evt:MouseEvent,index:int):void
		{
			if(viewGroup.getItemByIndex(index).data.CheckProperties("configID"))
			{
				var configID:int = viewGroup.getItemByIndex(index).data.GetProperties("configID");
				var num:String = viewGroup.getItemByIndex(index).data.GetProperties("num");
				switch(configID)
				{
					//代表银两
					case 0:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000538",[num]));
						break;
					case 7:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000540",[num]));
						break;
					case 2:
						TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000541",[num]));
						break;
					default:
						TipsManage.ShowAllItemsTips(int(StaticIni.getIniVar(String(configID),"ItemType")),configID);
						break;
				}
			}
			
				
		}
		
		private function onHideTip(evt:MouseEvent,index:int):void
		{
			ToolTips.hide();
		}
		
		
		/**
		 *动态修改充值的仙玉 
		 * 
		 */		
		public function set pGoldExp(val:int):void
		{
			_silver = val;
			if(_typeId!="")
			{
				modifyNeedMoney(_typeId);
			}
			
		}
		
		/**
		 *表的添加操作 
		 * 
		 */		
		private function onAddAwardGetRec(key:String, val:RecordObject):void
		{
			if(val!=null)
			{
				var type:int	=	val.GetProperties("0");
				var item:TopUpRewardItem = new TopUpRewardItem(type,_TypeListArray[_num]);
				_itemArray[int(key)] = item;
				_num++;
				if(_num==_TypeListArray.length)
				{
					addType();
				}
				//绑定改变了是否可以领取的状态
				bindManager.BindRecordProp(val,"1",changCanGet,[item]);
				//绑定了是否已经领取了
				bindManager.BindRecordProp(val,"2",changGetOrNot,[item]);
			}
		}
		
		/**
		 *添加不同类型的奖励在舞台上 
		 * 
		 */		
		private function addType():void
		{
			for(var i:int=0;i<_itemArray.length;i++)
			{
				var item:TopUpRewardItem = _itemArray[i] as TopUpRewardItem;
				if(i==0)
				{
					onSelect(item);
				}
				if(item!=null)
				{
					item.x = 10+(95)*i;
					item.y = 40;
					addChild(item);
					item.addEventListener(MouseEvent.CLICK,onShowGoods);
				}
			}
		}
		
		/**
		 *当改变是否可以领取的状态
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */		
		private function changCanGet(key:String,val:int,pList:Array):void
		{
			var item:TopUpRewardItem = pList[0] as TopUpRewardItem;
			if(val==1)
			{
				item.buttonState = true;
			}
			else
			{
				item.buttonState = false;
			}
		}
		
		/**
		 * 当改变了是否已经领取了
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */		
		private function changGetOrNot(key:String,val:int,pList:Array):void
		{
			var item:TopUpRewardItem = pList[0] as TopUpRewardItem;
			if(val==1)
			{
				item.buttonLabel = StaticIni.getSwfLangStr("4002006518");
				item.buttonState = false;
			}
		}
		
		/**
		 *表的删除操作 
		 * 
		 */		
		private function onremoveAwardGetRec(key:String, val:RecordObject):void
		{
			
		}
		
		private function onShowGoods(evt:MouseEvent):void
		{
			if(!(evt.currentTarget as TopUpRewardItem)._button.contains(evt.target as DisplayObject))
			{
				
				var item:TopUpRewardItem = evt.currentTarget as TopUpRewardItem;
				if(item.type == _nowType)
				{
					return;
				}
				_typeId= item.typeId;
				_nowType = item.type;
				//清除选中的效果
				clearAllSelect();
				//添加选中
				item.addSelect();
				//清除物品
				clearGoods();
				//添加物品
				showGoods(_typeId);
				//修改总价值得图片
				modifyTotelPhoto(_typeId);
				//修改所需要
				modifyNeedMoney(_typeId);
			}
		}
		
		/**
		 *默认选中第一个 
		 * 
		 */		
		private function onSelect(item:TopUpRewardItem):void
		{
			_nowType = item.type;
			_typeId = item.typeId;
			//清除选中的效果
			clearAllSelect();
			//添加选中
			item.addSelect();
			//清除物品
			clearGoods();
			//添加物品
			showGoods(item.typeId);
			//修改总价值得图片
			modifyTotelPhoto(item.typeId);
			//修改所需要
			modifyNeedMoney(item.typeId);
		}
		
		
		
		/**
		 *清除收有的选中效果 
		 * 
		 */		
		private function clearAllSelect():void
		{
			for(var i:int=0;i<_itemArray.length;i++)
			{
				var item:TopUpRewardItem = _itemArray[i] as TopUpRewardItem;
				if(item!=null)
				{
					item.clearSelect();
				}
			}
		}
		
		/**
		 *清除所有的物品 
		 * 
		 */		
		private function clearGoods():void
		{
			viewGroup.getItemForEach(function (tmpItem:BaseItem):void
			{
				tmpItem.clear();
				tmpItem.data.clearProperties();
			});
		}
		
		/**
		 *添加物品 
		 * 
		 */		
		private function showGoods(val:String):void
		{
			var type:String	=	"PrizeType";
			var num:String	=	"PrizeAmount";
			var icon:String = 	"PrizeId";
			for(var i:int = 1;i<=9;i++)
			{
				var realType:String = type+i.toString();
				var realNum:String = num+i.toString();
				var realIcon:String = icon +i.toString();
				var typeId:String = StaticIni.getIniVar(val,realType);
				var showNum:String = StaticIni.getIniVar(val,realNum);
				var configID:int  = int(StaticIni.getIniVar(val,realIcon));
				if(typeId==null)
				{
					break;	
				}
				else
				{
					addnum(i,val,realType,realIcon,showNum,configID);
				}
			}
		}
		
		/**
		 *对每个数据进行循环加载 
		 * @param i
		 * @param val
		 * @param realType
		 * @param realIcon
		 * @param showNum
		 * @param configID
		 * 
		 */		
		private function addnum(i:int,val:String,realType:String,realIcon:String,showNum:String,configID:int):void
		{
			var sprite:Sprite=new Sprite();
			var numlab:Label=new Label();
			var tmpItem:Clip=new Clip(true,function():void
			{
				sprite.addChild(numlab);
			},"jzLogo");
			tmpItem.source= confirmItemType(val,realType,realIcon);
			tmpItem.x	=	tmpItem.y	=	2;
			numlab.htmlStr = MoneyTools.setMoney(int(showNum));
			numlab.x 	= 54 - numlab.width;
			numlab.y = 30;
			sprite.addChild(tmpItem);
			sprite.x = sprite.y	=	0;
			viewGroup.getItemByIndex(i-1).addItem(sprite);
			viewGroup.getItemByIndex(i-1).data.HandleProperties("configID",configID);
			viewGroup.getItemByIndex(i-1).data.HandleProperties("num",MoneyTools.setMoney(int(showNum)));
		}
		
		/**
		 *确定奖励物品的类型 
		 * 
		 */		
		private function confirmItemType(typeId:String,type:String,icon:String):String
		{
			var str:String;
			var itemType:int = int(StaticIni.getIniVar(typeId,type));
			//类型号为0代表的是物品
			//类型号为1代表的是银两
			//类型是2代表的是仙玉
			//类型是3代表的是灵气
			switch(itemType)
			{
				case 0:
					var realIcon:String = StaticIni.getIniVar(typeId,icon);
					str = ResConfig.iconPath+StaticIni.getIniVar(realIcon,"Icon")+ResConfig.iconImg;
					break;
				case 1:
					str = ResConfig.pngResPath + "slive"+ResConfig.iconImg;
					break;
				case 2:
					str = ResConfig.pngResPath + "SP"+ResConfig.iconImg;
					break;
				case 3:
					str = ResConfig.pngResPath + "sprit"+ResConfig.iconImg;
					break;
				
			}
			return str;
		}
		
		/**
		 *修改总价值的图片 
		 * 
		 */		
		private function modifyTotelPhoto(val:String):void
		{
			valuePhoto.source = "";
			valuePhoto.source = ResConfig.pngResPath + StaticIni.getIniVar(val,"PictureId")+".png";
		}
		
		/**
		 *修改需要的仙玉 
		 * 
		 */		
		private function modifyNeedMoney(val:String):void
		{
			downLabel.htmlStr = "";
			var str:String = "TotalValue";
			var str1:String = "GoldAmount";
			var gold:int = int(StaticIni.getIniVar(val,str1));
			var totalValue:String = StaticIni.getIniVar(val,str);
			var needGold:int = gold<_silver?0:gold-_silver;
			downLabel.htmlStr = StaticIni.getSwfLangStrVar("1000000625",[MoneyTools.setMoney(needGold),MoneyTools.setMoney(int(totalValue))]);
		}
		
		/**
		 *当顶级充值按钮的时候打开网页进行充值 
		 * @param evt
		 * 
		 */		
		private function openWeb(evt:MouseEvent):void
		{
			
		}
	}
}